<?php
class ControllerWxLottery extends Controller{
    public function index(){


        require_once(DIR_SYSTEM . 'library/jssdk.php');
        $jssdk = new JSSDK("wxdc4ce97c601d543f", "f69bd9f2c11034a0fa5d4d2a527e0d9b");
        $signPackage = $jssdk->GetSignPackage();
        $access_token = $jssdk->getAccessToken();


        $open_id=$this->request->cookie['wx_openid'];

        $this->load->model('wx/wx');

        if($open_id){
            $lottery_times=$this->model_wx_wx->getlotterytimes($open_id);
        }else{
            $lottery_times=0;
        }


        $this->data['share_action']=$this->url->link('wx/home/share', '', 'SSL');
        $this->data['openid']=$open_id;
        $this->data['signPackage']=$signPackage;


        $this->data['lottery_times']=$lottery_times;

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/wx/lottery.tpl';
        } else {
            $this->template = 'default/template/wx/lottery.tpl';
        }


        $this->response->setOutput($this->render());

    }
    public function get_prize(){
        $open_id=$this->request->cookie['wx_openid'];

        $this->load->model('wx/wx');

        $customer_info=$this->model_wx_wx->getuserinfo($open_id);
        $json=array();

//        if(!$customer_info['subscribe']){
//            $json['error']='您还没有关注我们的公众号,请搜索neucloud关注我们。';
//        }elseif(!$customer_info['share_status']){
//            $json['error']='您还没有分享我们的活动,请点击右上方转发。';
//        }elseif($customer_info['lottery_times']<=0){
//            $json['error']='您的抽奖次数已经用完，快去推荐朋友注册获得抽奖次数吧！';
//        }


        if(!$json){
            $prize_arr = array(
                '0' => array('id'=>1,'prize'=>'Iphone6 Plus','v'=>0),
                '2' => array('id'=>3,'prize'=>'Ipad Air2','v'=>0),

                '4' => array('id'=>5,'prize'=>'乐扣水杯','v'=>5),
                '9' => array('id'=>10,'prize'=>'乐扣水杯','v'=>4),

                '13' => array('id'=>14,'prize'=>'50移动充值券','v'=>1),
                '3' => array('id'=>4,'prize'=>'100元移动移动券','v'=>0),
                '10' => array('id'=>11,'prize'=>'100元移动券','v'=>0),

                '6' => array('id'=>7,'prize'=>'1元微信红包','v'=>2),
                '7' => array('id'=>8,'prize'=>'2元微信红包','v'=>2),
                '14' => array('id'=>15,'prize'=>'10元微信红包','v'=>1),
                '11' => array('id'=>12,'prize'=>'100元微信红包','v'=>0),

                '1' => array('id'=>2,'prize'=>'10元寄云充值劵','v'=>10),
                '5' => array('id'=>6,'prize'=>'20元寄云充值劵','v'=>10),
                '12' => array('id'=>13,'prize'=>'50元寄云充值劵','v'=>5),

                '8' => array('id'=>9,'prize'=>'很遗憾没中哦','v'=>300),
                '15' => array('id'=>16,'prize'=>'很遗憾没中哦','v'=>300),
            );


            foreach ($prize_arr as $key => $val) {
                $arr[$val['id']] = $val['v'];
            }

            $rid = $this->get_rand($arr); //根据概率获取奖项id
            $json['prize']=$rid-1;
            $json['prize_name']=$prize_arr[$json['prize']]['prize'];
            //减去抽奖次数一次
            $this->model_wx_wx->subtractLotteryTimes($open_id);
            //记录奖品
            $this->model_wx_wx->addUserprize($open_id,$rid);
        }

        $this->response->setOutput(json_encode($json));
    }
    private function get_rand($proArr) {
        $result = '';

        //概率数组的总概率精度
        $proSum = array_sum($proArr);


        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);

            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }

        unset ($proArr);

        print_r($result);
        exit;
        return $result;
    }
    public function winner(){
        $this->load->model('wx/wx');
        if(!preg_match_all("/^1[3578]\d{9}$/",$this->request->post['telephone'],$array)){
            $json['msg']='手机号码格式错误。';
        }
        if ((utf8_strlen($this->request->post['name']) < 1) || (utf8_strlen($this->request->post['name']) > 32)) {
            $json['msg']='姓名不能为空。';
		}
        if(!$json){
            $this->model_wx_wx->updateWinnerInfo($this->request->cookie['wx_openid'],$this->request->post);
            $json['msg']='提交成功。';
        }
        $this->response->setOutput(json_encode($json));
    }

}
?>